let Pattern = {
  isMobile: /^1[3|4|5|6|7|8|9][0-9]{9}$/,
  isCnAndEn: /^[\u4E00-\u9FA5]+$/,
  isCnAndEnAndNum: /^[\u4e00-\u9fa5-a-zA-Z0-9]+$/,
  isUserName: /^[(\u4e00-\u9fa5)a-zA-Z]+$/,
  isPassword: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
  isAuthCode: /^[0-9]{6}$/,
  isTelAndMobile: /^(1[3|4|5|7|8][\d]{9}|0[\d]{2,3}-[\d]{7,8}|400[-]?[\d]{3}[-]?[\d]{4})$/
}

const validator = {
  /**
   * 执行正则表达式
   * @param pattern 校验的正则表达式
   * @param strValue 校验的值
   * @returns {boolean}
   */
  executeExp: function (pattern, strValue) {
    return pattern.test(strValue)
  },
  /**
   * 判断是否为中文
   * @param strValue 校验的值
   * @returns {boolean}
   */
  isCnAndEn: function (strValue) {
    if (this.isEmpty(strValue)) {
      return false
    }

    return this.executeExp(Pattern.isCnAndEn, strValue)
  },
  /**
   * 判断是否为中英文、数字及'-'
   * @param strValue 校验的值
   * @returns {boolean}
   */
  isCnAndEnAndNum: function (strValue) {
    if (this.isEmpty(strValue)) {
      return false
    }

    return this.executeExp(Pattern.isCnAndEnAndNum, strValue)
  },
  /**
   * 判断是否为手机号码
   * @param strValue 校验的值
   * @returns {boolean}
   */
  isMobile: function (strValue) {
    return this.executeExp(Pattern.isMobile, strValue)
  },
  /**
   * 判断是否为手机或电话号码
   * @param strValue 校验的值
   * @returns {boolean}
   */
  isTelAndMobile: function (strValue) {
    return this.executeExp(Pattern.isTelAndMobile, strValue)
  },
  /**
   * 是否在范围长度内
   * @param 范围数组 [1, 10] 长度在1-10内
   * @returns {boolean}
   */
  isLenRange: (strValue, lenArr) => {
    return strValue.length >= lenArr[0] && strValue.length <= lenArr[1]
  },
  /**
   * 是否在数值范围内
   * @param 范围数组 [1, 10] 数值在1-10内
   * @returns {boolean}
   */
  isNumRange: (numValue, numArr) => {
    return numValue >= numArr[0] && numValue <= numArr[1]
  }

}

function showLoading (title) {
  wx.showLoading({
    title
  })
}
// scence小程序码解析
function parseQuery (val) {
  let params = {}
  let seg = val.replace(/^\?/, '').split('&')
  let len = seg.length
  let p = null
  for (var i = 0; i < len; i++) {
    if (seg[i]) {
      p = seg[i].split('=')
      params[p[0]] = p[1]
    }
  }
  return params
}
// 时间倒计时转化
function getDateDiff (startTime, endTime, diffType) {
  // 将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式
  startTime = startTime.replace(/-/g, '/')
  endTime = endTime.replace(/-/g, '/')

  // 将计算间隔类性字符转换为小写
  diffType = diffType.toLowerCase()
  var sTime = new Date(startTime) // 开始时间
  var eTime = new Date(endTime) // 结束时间
  // 作为除数的数字
  var divNum = 1
  switch (diffType) {
    case 'second':
      divNum = 1000
      break
    case 'minute':
      divNum = 1000 * 60
      break
    case 'hour':
      divNum = 1000 * 3600
      break
    case 'day':
      divNum = 1000 * 3600 * 24
      break
    default:
      break
  }
  return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum))
}
// 时间戳格式化
function dateFormat (date, fmt) {
  var o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'q+': Math.floor((date.getMonth() + 3) / 3),
    'S': date.getMilliseconds()
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
}
// 订单维权码转化
function transOrderRoeStatus (status) {
  switch (status) {
    case 1:
      return '处理中'
    case 2:
      return '已同意'
    case 3:
      return '已拒绝'
    default:
      return '未知'
  }
}
// 物流状态转化
function transExpressTrackStatus (status) {
  switch (status) {
    case 'pending':
      return '查询中'
    case 'notfound':
      return '查询不到'
    case 'transit':
      return '运输途中'
    case 'pickup':
      return '到达待取'
    case 'delivered':
      return '成功签收'
    case 'undelivered':
      return '投递失败'
    case 'exception':
      return '可能异常'
    case 'expired':
      return '运输过久'
    default:
      return '暂无消息'
  }
}
function getDateDiffCountdown (time) {
  let dateTimeStamp = Date.parse(time)
  let result = this.dateFormat(new Date(time), 'MM-dd')
  let minute = 1000 * 60
  let hour = minute * 60
  let day = hour * 24
  let month = day * 30
  let now = new Date().getTime()
  let diffValue = now - dateTimeStamp
  if (diffValue < 0) return
  var monthC = diffValue / month
  let weekC = diffValue / (7 * day)
  let dayC = diffValue / day
  let hourC = diffValue / hour
  let minC = diffValue / minute
  if (monthC >= 1) {
    return result
  }
  if (weekC >= 1) {
    result = parseInt(weekC) + '周前'
    return result
  }
  if (dayC >= 1) {
    result = parseInt(dayC) + '天前'
    return result
  }
  if (hourC >= 1) {
    result = parseInt(hourC) + '小时前'
    return result
  }
  if (minC >= 1) {
    result = parseInt(minC) + '分钟前'
    return result
  }
  return '刚刚'
}

export default {
  validator,
  getDateDiff,
  dateFormat,
  parseQuery,
  transOrderRoeStatus,
  transExpressTrackStatus,
  showLoading,
  getDateDiffCountdown
}
export function transOrderStatus (status) {
  switch (status) {
    case 2:
      return '待支付'
    case 3:
      return '待发货'
    case 4:
      return '已发货'
    case 5:
      return '已完成'
    case 6:
      return '已关闭'
    default:
      return ''
  }
}
